<template>
  <!-- 选择物料对话框 -->
  <el-dialog
    :visible.sync="open"
    :title="title"
    :before-close="cancelSelect"
    width="900px"
    append-to-body
    :close-on-click-modal="false"
  >
    <div style="height: auto">
      <el-row>
        <el-col :span="6">
          <el-card>
            <div class="head-container">
              <el-input
                v-model="materialCategoryName"
                placeholder="请输入原料类别名"
                clearable
                size="small"
                prefix-icon="el-icon-search"
                style="margin-bottom: 20px"
              />
            </div>
            <div class="tree">
              <el-tree
                :data="categoryOptions"
                :props="defaultProps"
                :expand-on-click-node="false"
                :filter-node-method="filterNode"
                ref="tree"
                default-expand-all
                @node-click="handleNodeClick"
              />
            </div>
          </el-card>
        </el-col>
        <el-col :span="18">
          <el-card>
            <el-form
              :model="queryParams"
              ref="queryMaterialForm"
              :inline="true"
              label-width="68px"
            >
              <el-row>
                <el-col :span="24">
                  <el-form-item label="快捷码" prop="shortcutCode">
                    <el-input
                      v-model="queryParams.shortcutCode"
                      placeholder="请输入快捷码"
                      clearable
                      size="small"
                      @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item margin-bottom="7px">
                    <el-button
                      type="cyan"
                      icon="el-icon-search"
                      size="mini"
                      @click="handleQuery"
                      >搜索</el-button
                    >
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>

            <el-table
              height="245px"
              ref="mt"
              v-loading="loading"
              :data="materialList"
              @selection-change="handleSelectionChange"
            >
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column
                label="原料编号"
                align="center"
                prop="materialNo"
              />
              <el-table-column
                label="原料名称"
                align="center"
                prop="materialName"
              />
              <el-table-column
                label="快捷码"
                align="center"
                prop="shortcutCode"
              />
              <el-table-column
                label="原料类别"
                align="center"
                prop="materialCategoryName"
              />
              <el-table-column
                label="原料规格"
                align="center"
                prop="materialSpec"
              />
              <el-table-column
                label="原料单位"
                align="center"
                prop="materialUnit"
                :formatter="materialUnitFormat"
              />
            </el-table>

            <pagination
              v-show="total > 0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />
            <el-form label-width="100px">
              <el-form-item
                style="text-align: center; margin-left: -100px; margin-top: 50px"
              >
                <el-button type="primary" @click="confirmSelect"
                  >确 定</el-button
                >
                <el-button @click="cancelSelect">取 消</el-button>
              </el-form-item>
            </el-form>
          </el-card>
        </el-col>
      </el-row>

      <!-- <el-col :span="20" :xs="24"> -->

      <!-- </el-col> -->
    </div>
  </el-dialog>
</template>

<script>
import {
  listMaterial,
  getMaterial,
  delMaterial,
  addMaterial,
  updateMaterial,
  exportMaterial
} from "@/api/systembasic/material";
import { treeselect } from "@/api/systembasic/materialCategory";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "SelectMaterialDialog",
  components: { Treeselect },
  props: {
    open: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      title: "",
      // 原料单位字典
      materialUnitOptions: [],
      materialCategoryName: undefined,
      categoryOptions: [],
      // 原料管理表格数据
      materialList: [],
      selectedMaterial: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shortcutCode: null,
        materialCategoryId: null
      },
      defaultProps: {
        children: "children",
        label: "label"
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    materialCategoryName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.title = "选择物料";
    this.getDicts("smes_unit").then(response => {
      this.materialUnitOptions = response.data;
    });
    this.getList();
    this.getTreeselect();
  },

  methods: {
    /** 查询原料管理列表 */
    getList() {
      this.loading = true;
      listMaterial(this.queryParams).then(response => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 原料单位字典翻译
    materialUnitFormat(row, column) {
      return this.selectDictLabel(this.materialUnitOptions, row.materialUnit);
    },
    getTreeselect() {
      treeselect().then(response => {
        this.categoryOptions = response.data;
        console.log(this.categoryOptions);
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.materialCategoryId = data.id;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleSelectionChange(selection) {
      this.selectedMaterial = null;
      if (selection.length > 0) {
        this.selectedMaterial = selection[0];
        this.selectedMaterial.materialUnit = this.materialUnitFormat(
          this.selectedMaterial
        );
      }
      if (selection.length > 1) {
        this.$refs.mt.clearSelection();
        this.$refs.mt.toggleRowSelection(selection.pop());
      }
    },
    // 选中物料
    confirmSelect() {
      this.$emit("select", this.selectedMaterial);
    },
    // 取消选中
    cancelSelect() {
      this.$emit("cancel");
    }
  }
};
</script>
<style scoped>
.el-tree {
  min-width: 100%;
  display: inline-block;
}
.tree {
  overflow:auto;
  max-height: 400px;
}
</style>
